﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Data;
using System.Xml;
using System.Configuration;
using Microsoft.VisualBasic.ApplicationServices;

namespace FatController
{
    public class EntryPoint
    {
        [STAThread]
        public static void Main(string[] args)
        {
            SingleInstanceManager InstanceManager = new SingleInstanceManager();
            InstanceManager.Run(args);
        }
    }

    public class SingleInstanceManager : WindowsFormsApplicationBase
    {
        private SingleInstanceApplication FatControllerApplication;

        public SingleInstanceManager()
        {
            this.IsSingleInstance = true;
        }

        protected override bool OnStartup(Microsoft.VisualBasic.ApplicationServices.StartupEventArgs e)
        {
            FatControllerApplication = new SingleInstanceApplication();
            FatControllerApplication.RunWithCommandLineArguments(e.CommandLine);
            
            return false;
        }

        protected override void OnStartupNextInstance(StartupNextInstanceEventArgs eventArgs)
        {
            base.OnStartupNextInstance(eventArgs);
            FatControllerApplication.Activate(eventArgs.CommandLine);
        }
    }

    public class SingleInstanceApplication : Application
    {
        private WindowMain WindowMainWindow;

        private IEnumerable<string> CommandLineArguments;

        protected override void OnStartup(System.Windows.StartupEventArgs e)
        {
            base.OnStartup(e);
            WindowMainWindow = new WindowMain();
            WindowMainWindow.Show();
            WindowMainWindow.ProcessCommandLine(CommandLineArguments);
        }

        public void Activate(IEnumerable<string> CommandLineArguments)
        {
            WindowMainWindow.ProcessCommandLine(CommandLineArguments);
        }

        public void RunWithCommandLineArguments(IEnumerable<string> CommandLineArguments)
        {
            this.CommandLineArguments = CommandLineArguments;
            this.Run();
        }
    }
}
